<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_api_shop',$params,array($pindex,$psize),$total,'','',$orderby);
	if($list){
		$url=base64_encode('core/app_release.myshop.plugins//isapi=1');
		foreach($list as &$row){
			$row['createtime']=date('20y-m-d',$row['createtime']);
			$row['api_url']='core/cloud.store//api_id='.$row['id'].'&url='.$url;
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'市场列表'),
			array('op'=>'api_shops','title'=>'云市场'),
			array('op'=>'edit','title'=>'添加市场'),
		),
		'list'=>array('tip'=>'[<red>温馨提示</red>]：</br>1、前往第三方市场注册之后，即可获取相应对接参数！</br>2、只有绑定成功的，第三方应用市场购买安装的应用，才会关联本平台！</br>3、<red>所有第三方市场，最终责任和解释权都归第三方市场</red>。','data'=>'list','list'=>array(
			array('type'=>'string','name'=>'username','title'=>'第三方应用市场账号','read'=>true),
			array('type'=>'string','name'=>'url','title'=>'第三方应用市场接口url'),
			array('type'=>'string','title'=>'创建时间','name'=>'createtime'),
		),'action'=>array(
			array('type'=>'href_item','action'=>'api_url','title'=>'前往第三方应用市场'),
		)),
		'search_one'=>array('name'=>'title','add_query'=>'type=0'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='api_shops'){
	$params=array();
	if($_RQ['title']){
		$params['title']=$_RQ['title'];
	}
	if($_RQ['page']){
		$params['page']=$_RQ['page'];
	}
	$return=cloud_request('update.developer.shops',$params);
	if($return['list']){
		$url=base64_encode('core/app_release.myshop.plugins//isapi=1');
		foreach($return['list'] as &$row){
			$myshop_domain=explode('/',$row['myshop_domain']);
			$myshop_domain=$myshop_domain[2];
			$row['api_id']=pdo_getcolumn('core_api_shop',array('domain'=>$myshop_domain),'id');
			$row['is_install']=$row['api_id']?1:0;
			$row['api_url']='core/cloud.store//api_id='.$row['api_id'].'&url='.$url;
		}
	}
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_api_shop',array('id'=>$id));
	}else{
		$item=array('type'=>intval($_RQ['type']));
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'编辑市场',
		'tabs'=>array(
			array('op'=>'list','title'=>'市场列表'),
			array('op'=>'edit','title'=>'编辑市场'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'username','title'=>'第三方市场账号','help'=>'移步第三方市场注册账户。注册成功后登陆进入后台——站点设置——我的站点——云接口参数获取。'),
			array('type'=>'string','name'=>'domain','title'=>'第三方市场接口域名','help'=>'移步第三方市场注册账户。注册成功后登陆进入后台——站点设置——我的站点——云接口参数获取。'),
			array('type'=>'string','name'=>'url','title'=>'第三方市场接口url','help'=>'移步第三方市场注册账户。注册成功后登陆进入后台——站点设置——我的站点——云接口参数获取。'),
			array('type'=>'string','name'=>'cloudkey','title'=>'第三方市场接口token','help'=>'移步第三方市场注册账户。注册成功后登陆进入后台——站点设置——我的站点——云接口参数获取。'),
			array('type'=>'string','name'=>'secret','title'=>'第三方市场接口密钥','help'=>'移步第三方市场注册账户。注册成功后登陆进入后台——站点设置——我的站点——云接口参数获取。'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['username'] || !$post['url'] || !$post['cloudkey'] || !$post['secret'] || !$post['domain']){
		exi('所有参数必填！','error');
	}
	$data=array(
		'username'=>trim($post['username']),
		'url'=>trim($post['url']),
		'cloudkey'=>trim($post['cloudkey']),
		'secret'=>trim($post['secret']),
		'domain'=>trim($post['domain']),
	);
	$id=intval($post['id']);
	$res=api_request('api_shop.check',$data,array());
	if($res=='success'){
		if($id){
			pdo_update('core_api_shop',$data,array('id'=>$id));
		}else{
			$data['createtime']=TIMESTAMP;
			pdo_insert('core_api_shop',$data);
		}
		exi('保存成功！','','list');
	}else{
		exi('保存失败！第三方云接口参数异常！','error');
	}
}
//一键添加市场
if($_RQ['op']=='api_add'){
	$post=get_postdata();
	$data=array(
		'username'=>$_SERVER['HTTP_HOST'],
		'secret'=>'open_insecret',
		'cloudkey'=>'open_incloudkey',
		'url'=>$post['myshop_domain'].'api/index.php'
	);
	$params=array(
		'domain'=>$_SERVER['HTTP_HOST'],
		'api_userkey'=>random(43),
	);
	$myshop_domain=explode('/',$post['myshop_domain']);
	$myshop_domain=$myshop_domain[2];
	if($myshop_domain == $_SERVER['HTTP_HOST']){
		exi('您添加自己的应用市场！','error');
	}
	$apiid=pdo_getcolumn('core_api_shop',array('domain'=>$myshop_domain),'id');
	if($apiid){
		exi('您已添加过该市场！','error');
	}
	$res=api_request('api_shop.open_in',$data,$params);
	$data=array(
		'username'=>trim($res['username']),
		'url'=>trim($res['url']),
		'cloudkey'=>trim($res['token']),
		'secret'=>trim($res['aeskey']),
		'domain'=>trim($res['domain']),
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('core_api_shop',$data);
	exi('添加成功！','','list');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_api_shop',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			if(intval($id)){
				pdo_delete('core_api_shop',array('id'=>intval($id)));
			}
		}
	}
	exi('删除成功！');
}